/*
 * Copyright (C) 2013 tangzhi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.tangzsoft.pdmviewer;

import java.io.File;
import javax.swing.JOptionPane;
import org.tangzsoft.pdmviewer.model.PdmFile;
import org.tangzsoft.pdmviewer.parser.PdmParser;
import org.tangzsoft.pdmviewer.storage.Storage;
import org.tangzsoft.pdmviewer.utils.Utils;

/**
 * 主窗口MainView的控制器
 *   主窗口上导航树的控制器见NavTreeControler
 * 
 * @author tangzhi
 */
public class InterfaceControler {
    
    /**
     * 主窗体
     */
    private MainView mainView;
    
    
    
    ///////////////////////////////////////////
    
    private PdmParser pdmParser;
    
    private Storage storage =  PdmViewerContext.getInstance().getStorage();
    
    
    //////////////////////////////////////////
    
    
    
    
    
    
    
    public InterfaceControler(MainView view) {
        mainView = view;
        pdmParser = new PdmParser(view);
    }
    
    
    
    /**
     * 打开pdm文件事件
     * @param evt 
     */
    public void fnOpenFile(java.awt.event.ActionEvent evt) {
        
        final String[][] exts = {{"*","All file"}, {"pdm", "PDM file"}};
        
        //选择文件
        File file;
        file = Utils.openFile(mainView, exts);
        //file = new File("/Users/tangzhi/Documents/BSS数据模型/test15.pdm");
        
        //判断当前文件是否已经在列表中存在，则询问是否重新打开
        NavTreeControler.FileState fs;
        if ((fs = mainView.ntc.fileExists(file.getAbsolutePath()))==NavTreeControler.FileState.OPEN) {
            //如果存在且状态打开则询问
            Object[] options = {"Yes, please","No!"};
                    
            int n = JOptionPane.showOptionDialog(mainView,
                "Would you reopen the file?\n\n"+file.getAbsolutePath(),
                "Reopen the file",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,     //do not use a custom Icon
                options,  //the titles of buttons
                options[0]); //default button title

            if (n == JOptionPane.YES_OPTION) {
                //选择重新打开
                //TODO:删除存储中原有文件
                storage.getPdmFileDAO().remove(file.getAbsolutePath());
                
            }
        } 
        
        //若文件在列表中存在，todo
        if (fs != NavTreeControler.FileState.NOEXISTS) {
            
        }
        
        //读取文件.读取文件时不判断是否已经打开.
        if (file != null) {
            try{
                PdmFile pdm = pdmParser.parserFile(file);
                
                //添加节点
                mainView.ntc.addFile(pdm);
                    
            }catch(Exception ex) {
            JOptionPane.showMessageDialog(mainView,
                "Error:"+ex.getMessage(),
                "Open File Error",
                JOptionPane.ERROR_MESSAGE);
        }
        }
    }
    
    
    
}
